// pages/stepdonate/stepdonate.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    company: null,
    date: '',
    timeStamp: '',
    step: '',
    sportInfo:null
  },
  // 捐赠
  donation() {
    let userInfo = wx.getStorageSync('userInfo')
    let userId = wx.getStorageSync('userId')
    wx.login({
      success: res => {
        let code = res.code
        if (userInfo == '' || userInfo == '') {
          wx.getUserInfo({
            success: res => {
              wx.setStorageSync('userInfo', res.userInfo)
              wx.request({
                url: getApp().globalData.api + '/a/wxinterface/setUserInfo', //
                data: {
                  js_code: code,
                  encryptedData: res.encryptedData,
                  iv: res.iv
                },
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success: res => {
                  wx.setStorageSync('userId', res.data.obj.userId)
                  this.donationFun();
                }
              })
            }
          })
        } else {
          this.donationFun();
        }
      }
    })
  },
  donationFun(){
    if (!this.data.company) {
      wx.showModal({
        title: '提示',
        content: '未设置企业信息，无法捐赠，请联系管理员。！',
        showCancel: false
      })
      return
    }
    if (this.data.step < 10000) {
      wx.showModal({
        title: '提示',
        content: '最低捐赠步数为1万步，并且每日只能捐赠一次！',
        showCancel: false
      })
      return
    }
    let _this = this
    wx.request({
      url: getApp().globalData.api + '/a/wxinterface/qinglian/donateStep',
      data: {
        userId: wx.getStorageSync('userId'),
        eventId: getApp().globalData.eventId,
        step: _this.data.step,
        setpTime: _this.data.timeStamp
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: res => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000,
            success: _this.getCompany
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.errmsg,
            showCancel: false
          })
        }
      }
    })
  },
  // 时间选择
  datepre() {
    let timeStamp = this.data.timeStamp
    timeStamp -= 60 * 60 * 24 * 1000
    let date = util.formatTimeZH(new Date(timeStamp))
    if (!this.checkTime(date)) return;
    this.setData({
      timeStamp: timeStamp,
      date: date
    })
    this.getStep(timeStamp)
  },
  datenext() {
    let timeStamp = this.data.timeStamp
    timeStamp += 60 * 60 * 24 * 1000
    let date = util.formatTimeZH(new Date(timeStamp))
    if (!this.checkTime(date)) return;
    this.setData({
      timeStamp: timeStamp,
      date: date
    })
    this.getStep(timeStamp)
  },
  // 获取步数信息
  getStep(timeStamp) {
    let code = '';
    let _this = this
    if (this.data.sportInfo == null) {
      _this.getRunData()
    }else{
      _this.setStep()
    }  
  },
  //获取微信运行信息
  getRunData(){
    let code = ''
    let _this = this
    wx.login({
      success: res => {
        code = res.code
      }
    })
    wx.getWeRunData({
      success: res => {
        wx.request({
          url: getApp().globalData.api + '/a/wxinterface/qinglian/getSportInfo',
          data: {
            code: code,
            encryptedData: res.encryptedData,
            iv: res.iv
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: res => {
            _this.setData({
              sportInfo: res.data.obj.stepInfoList.stepInfoList
            })
            _this.setStep()
          }
        })
      }
    })
  },
  // 获取企业信息
  getCompany() {
    let _this = this
    wx.request({
      url: getApp().globalData.api + '/a/wxinterface/qinglian/getCompany',
      data: {
        eventId: getApp().globalData.eventId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: res => {
        if (res.data.obj == null){
          wx.showModal({
            title: '提示',
            content: res.data.errmsg,
            showCancel: false
          }) 
        }else{
          let money = res.data.obj.companyLeft
          let logo = res.data.obj.companyLogo.replace('/qqc', '')
          if (money > 100) {
            money = (money / 10000).toFixed(2) + '万'
          }
          let company = {
            name: res.data.obj.companyName,
            logo: getApp().globalData.api + logo,
            left: money,
            rule: res.data.obj.ruleTxt
          }
          _this.setData({
            company: company
          })
        }
      }
    })
  },
  // 获取当前时间信息
  getDate() {
    let timeStamp = +new Date()
    let date = util.formatTimeZH(new Date(timeStamp))
    this.setData({
      timeStamp: timeStamp,
      date: date
    })
    this.getStep(timeStamp)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getDate()
    this.getCompany()
  },
  setStep() {
    let _date = '';
    for (let i = 0; i < this.data.sportInfo.length; i++) {
      _date = util.formatTimeZH(new Date(this.data.sportInfo[i].timestamp * 1000))
      if (_date == this.data.date) {
        this.setData({
          step: this.data.sportInfo[i].step
        })
        return
      }
    }
  },
  checkTime(date){
    let boo = false
    let _date = '';
    for (let i = 0; i < this.data.sportInfo.length; i++) {
      _date = util.formatTimeZH(new Date(this.data.sportInfo[i].timestamp * 1000))
      if (_date == date) {
        boo = true
      }
    }
    return boo
  }
  
})